document.addEventListener('DOMContentLoaded', function() {
    var questionInput = document.getElementById('questionInput');
    var askButton = document.getElementById('askButton');
    var answerContainer = document.getElementById('answerContainer');
    
    askButton.addEventListener('click', function() {
        var question = questionInput.value;
        fetch('http://127.0.0.1:5000/ask', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ question: question })
        })
        .then(response => response.json())
        .then(data => {
            answerContainer.innerHTML = '<p>' + data.answer + '</p>';
        })
        .catch(error => {
            console.error('Error:', error);
            answerContainer.innerHTML = '<p>发生错误，请稍后重试。</p>';
        });
    });
});